package com.changgou.user.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.common.util.JwtUtil;
import com.changgou.user.pojo.User;
import com.changgou.user.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 添加用户积分
     * @param points
     * @return
     */
    @GetMapping(value = "/points/add")
    public Result addPoints(@RequestParam Integer points){
        userService.addPoints(points,"heima");
        return new Result(true, StatusCode.OK,"添加成功") ;

    }
    /**
     * 用户登录
     * @return
     */
    @GetMapping(value = "/login")
    public Result login(@RequestParam String username, @RequestParam String password, HttpServletResponse response){
        User user = userService.findById(username);
        // 判断密码是否相等
        if (user!=null){
            if (common.security.BCrypt.checkpw(password,user.getPassword())){
                // 创建用户的token
                Map<String,Object> info= new HashMap<>();
                info.put("role", "USER");
                info.put("success", "SUCCESS");
                info.put("username", username);
                String token = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(info), null);
                //把令牌信息存入token
                Cookie cookie = new Cookie("Authorization", token);
                cookie.setDomain("localhost");
                cookie.setPath("/");
                response.addCookie(cookie);
                return new Result(true, StatusCode.OK,"登录成功",token) ;
            }
        }
        return new Result(true, StatusCode.LOGINERROR,"账号或者密码错误") ;
    }

    /**
     * 查询全部数据
     * @return
     *  @PreAuthorize("hasAnyRole(user','admin','vip')") 配置可以访问的角色
     */
    @GetMapping
    public Result findAll(){
        List<User> userList = userService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",userList) ;
    }
    /***
     * 根据用户名查询数据
     * @param username 配置多个访问路径
     * @return
     */
    @GetMapping({"/{username}" })
    public Result<User> findById(@PathVariable String username){
        User user = userService.findById(username);
        return new Result<User>(true,StatusCode.OK,"查询成功",user);
    }


    /***
     * 新增数据
     * @param user
     * @return
     */
    @PostMapping
    public Result add(@RequestBody User user){
        userService.add(user);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param user
     * @param username
     * @return
     */
    @PutMapping(value="/{username}")
    public Result update(@RequestBody User user,@PathVariable String username){
        user.setUsername(username);
        userService.update(user);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param username
     * @return
     */
    @DeleteMapping(value = "/{username}" )
    public Result delete(@PathVariable String username){
        userService.delete(username);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<User> list = userService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<User> pageList = userService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


}
